package com.nondo.dean.platform.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.platform.domain.Province;
import com.nondo.dean.platform.domain.ProvinceExample;
import com.nondo.dean.platform.mapper.ProvinceMapper;
import com.nondo.dean.platform.model.AreaBean;

/**
 * 省份管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月3日
 */
@Component
public class ProvinceService {

    @Autowired
    private ProvinceMapper provinceMapper;

    /**
     * 插入省份
     * @param province
     * @return
     */
    @Transactional
    public int addProvince(Province province) {
        return provinceMapper.insertSelective(province);
    }

    /**
     * 修改省份
     * @param province
     * @return
     */
    @Transactional
    public int updateProvince(Province province) {
        return provinceMapper.updateByPrimaryKeySelective(province);
    }

    /**
     * 按照主键ID查询省份
     * @param id
     * @return
     */
    public Province selectProvince(Integer id) {
        return provinceMapper.selectByPrimaryKey(id);
    }

    /**
     * 省份分页查询列表
     * @param offset
     * @param limit
     * @param province
     * @return
     */
    public List<Province> getProvinceList(int offset, int limit, Province province) {
        ProvinceExample example = new ProvinceExample();
        RowBounds rowBounds = new RowBounds(offset, limit);
        ProvinceExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, province);
        return provinceMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 省份总数统计
     * @param province
     * @return
     */
    public Long count(Province province) {
        ProvinceExample example = new ProvinceExample();
        ProvinceExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, province);
        return provinceMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param province
     */
    void setCriteria(ProvinceExample.Criteria criteria, Province province) {
        if (province != null) {
            if (province.getId() != null)
                criteria.andIdEqualTo(province.getId());
            if (StringUtils.isNoneBlank(province.getName()))
                criteria.andNameLike("%" + province.getName() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

   /**
    * 按照省份名称查询省份
    * @param name
    * @return
    */
    public Province selectProvinceByName(String name) {
        ProvinceExample example = new ProvinceExample();
        ProvinceExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Province> provinceList = provinceMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(provinceList)?provinceList.get(0):null;
    }
    
    /**
     * 省份列表
     * @return
     */
    public List<AreaBean> getProvinceList() {
        List<AreaBean> resultList = new ArrayList<AreaBean>();
        ProvinceExample example = new ProvinceExample();
        ProvinceExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Province> provinceList = provinceMapper.selectByExample(example);
        for (Province province : provinceList) {
            AreaBean provinceBean = AreaBean.convertToBean(province);
            resultList.add(provinceBean);
        }
        return resultList;
    }
}
